import math
try:
    from ulab import numpy as np
except ImportError:
    import numpy as np

print("Testing np.min:")
print(np.min([1]))
print(np.min(np.array([1], dtype=np.float)))
a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.uint8)
print(np.min(a))
print(np.min(a, axis=0))
print(np.min(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint8)
print(np.min(a))
print(np.min(a, axis=0))
print(np.min(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int8)
print(np.min(a)) ## Problem here
print(np.min(a, axis=0))
print(np.min(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint16)
print(np.min(a))
print(np.min(a, axis=0))
print(np.min(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int16)
print(np.min(a))
print(np.min(a, axis=0))
print(np.min(a, axis=1))
a = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.min(a))
print(np.min(a, axis=0))
print(np.min(a, axis=1))

print("Testing np.max:")
print(np.max([1]))
print(np.max(np.array([1], dtype=np.float)))
a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.uint8)
print(np.max(a))
print(np.max(a, axis=0))
print(np.max(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint8)
print(np.max(a))
print(np.max(a, axis=0))
print(np.max(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int8)
print(np.max(a)) ## Problem here
print(np.max(a, axis=0))
print(np.max(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint16)
print(np.max(a))
print(np.max(a, axis=0))
print(np.max(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int16)
print(np.max(a))
print(np.max(a, axis=0))
print(np.max(a, axis=1))
a = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.max(a))
print(np.max(a, axis=0))
print(np.max(a, axis=1))

print("Testing np.argmin:")
print(np.argmin([1]))
print(np.argmin(np.array([1], dtype=np.float)))
a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.uint8)
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint8)
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int8)
print(np.argmin(a)) ## Problem here
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint16)
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int16)
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))
a = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.argmin(a))
print(np.argmin(a, axis=0))
print(np.argmin(a, axis=1))

print("Testing np.argmax:")
print(np.argmax([1]))
print(np.argmax(np.array([1], dtype=np.float)))
a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.uint8)
print(np.argmax(a))
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint8)
print(np.argmax(a))
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int8)
print(np.argmax(a)) ## Problem here
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.uint16)
print(np.argmax(a))
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))
a = np.array([range(255-5, 255),range(240-5, 240),range(250-5,250)], dtype=np.int16)
print(np.argmax(a))
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))
a = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.argmax(a))
print(np.argmax(a, axis=0))
print(np.argmax(a, axis=1))

print("Testing np.minimum:")
print(np.minimum(10, 9))
print(np.minimum(10.0, 9.0))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
b = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.minimum(a, b))

print("Testing np.maximum:")
print(np.maximum(a, b))
print(np.maximum(10, 9))
print(np.maximum(10.0, 9.0))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
b = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.maximum(a, b))

print("Testing np.sort:")
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.uint8)
b = np.array([range(2**56-3, 2**56),range(2**16-3, 2**16),range(2**8-3, 2**8)], dtype=np.float)
print(np.sort(a, axis=None))
print(np.sort(b, axis=None))
print(np.sort(a, axis=0))
print(np.sort(b, axis=0))
print(np.sort(a, axis=1))
print(np.sort(b, axis=1))

print("Testing np.sum:")
a = np.array([253, 254, 255], dtype=np.uint8)
print(np.sum(a))
print(np.sum(a, axis=0))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
print(np.sum(a))
print(np.sum(a, axis=0))
print(np.sum(a, axis=1))

print("Testing np.mean:")
a = np.array([253, 254, 255], dtype=np.uint8)
print(np.mean(a))
print(np.mean(a, axis=0))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
#print(np.mean(a))
print(math.isclose(np.mean(a), 246.3333333333333, rel_tol=1e-06, abs_tol=1e-06))
#print(np.mean(a, axis=0))
result = np.mean(a, axis=0)
ref_result = [245.33333333, 246.33333333, 247.33333333]
for p, q in zip(list(result), ref_result):
    print(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06))

#print(np.mean(a, axis=1))
result = np.mean(a, axis=1)
ref_result = [253., 238., 248.]
for p, q in zip(list(result), ref_result):
    print(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06))

print("Testing np.std:")
a = np.array([253, 254, 255], dtype=np.uint8)
#print(np.std(a))
print(math.isclose(np.std(a), 0.816496580927726, rel_tol=1e-06, abs_tol=1e-06))
print(math.isclose(np.std(a, axis=0), 0.816496580927726, rel_tol=1e-06, abs_tol=1e-06))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
#print(np.std(a))
print(math.isclose(np.std(a), 6.289320754704403, rel_tol=1e-06, abs_tol=1e-06))
#print(np.std(a, axis=0))
result = np.std(a, axis=0)
ref_result = [6.23609564, 6.23609564, 6.23609564]
for p, q in zip(list(result), ref_result):
    print(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06))

#print(np.std(a, axis=1))
result = np.std(a, axis=1)
ref_result = [0.81649658, 0.81649658, 0.81649658]
for p, q in zip(list(result), ref_result):
    print(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06))

print("Testing np.median:")
a = np.array([253, 254, 255], dtype=np.uint8)
print(np.median(a))
print(np.median(a, axis=0))
a = np.array([range(255-3, 255),range(240-3, 240),range(250-3,250)], dtype=np.float)
print(np.median(a))
print(np.median(a, axis=0))
print(np.median(a, axis=1))
print("Testing np.roll:")  ## Here is problem
print(np.arange(10))
print(np.roll(np.arange(10), 2))
print(np.roll(np.arange(10), -2))
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(np.roll(a, 2))
print(np.roll(a, -2))
print("Testing np.clip:")
print(np.clip(5, 3, 6))  ## Here is problem
print(np.clip(7, 3, 6))
print(np.clip(1, 3, 6))
a = np.array([1,2,3,4,5,6,7], dtype=np.int8)
print(np.clip(a, 3, 5))
a = np.array([1,2,3,4,5,6,7], dtype=np.float)
print(np.clip(a, 3, 5))
